package com.googlecode.openauth.web.action;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.googlecode.openauth.web.dao.IUserDao;
import com.googlecode.openauth.web.entity.UserInfo;
import com.googlecode.openauth.web.util.Util;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ChangePwdAction extends ActionSupport {
private String oldPwd;
private String newPwd;
private IUserDao userDao;
private String result;
private UserInfo getCurUser() {
	ActionContext context = ActionContext.getContext();
	Map<String, Object> session = context.getSession();
	Object obj = session.get("user");
	UserInfo result = new UserInfo();
	result.setUserId(-1);
	if (obj == null) {
		
		return result;
	}

	if (!(obj instanceof UserInfo)) {
		return result;
	}

	result = (UserInfo) obj;
	return result;
}
@Override
public String execute() throws Exception {
	// TODO Auto-generated method stub
	String encodedPassword = Util.encode(oldPwd);

	UserInfo user = userDao.getUserByName(getCurUser().getUsername());
	if(null!=user)
	{
		String dbPassword = user.getPassword();
		if (StringUtils.isEmpty(dbPassword)) {
			result="nsucc";
			return "unsuccess";
		}
		if (dbPassword.equals(encodedPassword))
		{
			user.setPassword(Util.encode(newPwd));
			userDao.updateUser(user);
			result="succ";
		}
		return "success";
	}
	else
	{
		result="nsucc";
		return "unsuccess";
	}
	
}
public String getOldPwd() {
	return oldPwd;
}
public void setOldPwd(String oldPwd) {
	this.oldPwd = oldPwd;
}
public String getNewPwd() {
	return newPwd;
}
public void setNewPwd(String newPwd) {
	this.newPwd = newPwd;
}
public IUserDao getUserDao() {
	return userDao;
}
public void setUserDao(IUserDao userDao) {
	this.userDao = userDao;
}
public String getResult() {
	return result;
}
public void setResult(String result) {
	this.result = result;
}

}
